/*
  File:     PullDataSource.cpp

  history:

*/

#include "PullDataSource.h"
#include "PullDataRecv.h"
#include "TimeBuffer.h"
#include "XMLConfig2.h"
#include "Globle.h"
#include "SSMonitorItems.h"

CPullDataSource::CPullDataSource(CBusinessDNA& inBusinessDNA):CDataSource(inBusinessDNA)
{
	m_pPullRecv = 0;

	STAT_COUNT_INC(this);
}

CPullDataSource::~CPullDataSource()
{
	OutputDebugString("CPullDataSource::~CPullDataSource\n");
	if( m_pPullRecv )
	{
		printf("IN PullDataSource-->Stop PullDataRecv Object,Delete PullDataSource!\n");
		m_pPullRecv->DeleteTask();
		m_pPullRecv = 0;
	}

	STAT_COUNT_DEC(this);
}

Bool  CPullDataSource::Initialize()
{
	OSMutexLocker Locker(&m_Mutex);
	
	if(m_Status > STAT_NOREADY)
		return TRUE;

	if( !CDataSource::Initialize() )
		return FALSE;

	if( m_cBusinessDNA.GetTopModel() == T_P2P )
	{
		UInt32 nUrgentRange = SConfigPara::m_UrgentRange;
		nUrgentRange = m_pTimeBuffer->GetBufferingScale();
		
		if(!m_pTimeBuffer->Initialize(SConfigPara::m_sliceSize, SConfigPara::m_bufLimit, nUrgentRange))
			return FALSE;
	}
	
	m_pPullRecv = new CPullDataRecv(m_cBusinessDNA);
	if( !m_pPullRecv )
		return FALSE;
	
	Assert( !m_strSDP.empty() );
	m_pPullRecv->SetSDP( m_strSDP );
	m_pPullRecv->SetFD(m_FD);
	m_pPullRecv->SetTimeBuffer( m_pTimeBuffer );

	m_pPullRecv->SetDataSource(this);
	
	if( !m_pPullRecv->Initialize() )
		return FALSE;
	
	m_Status = STAT_INIT;
		
	return TRUE;
}

Bool  CPullDataSource::Seek(UInt64 vStart)
{
	return TRUE;
}

Bool  CPullDataSource::Pause()
{
	return TRUE;
}

Bool  CPullDataSource::Stop()
{
	OSMutexLocker Locker(&m_Mutex);

	m_pPullRecv->Stop();
	
	m_Status = STAT_STOP;
	
	return TRUE;
}

Bool CPullDataSource::AddConnect(Int32 fd)
{
	OSMutexLocker Locker(&m_Mutex);
	Bool bRet = m_pPullRecv->AddConnect(fd);
	return bRet;
}
